package com.elong.android.specialhouse.wxapi;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class WXSharedPreferencesTools {

	final static private String FILE_NAME = "WXSharedPreferencesTools";

	private WXSharedPreferencesTools() {
	}

	private static WXSharedPreferencesTools instance;

	public static synchronized WXSharedPreferencesTools getInstance() {
		if (instance != null) {
			return instance;
		}
		instance = new WXSharedPreferencesTools();
		return instance;
	}

	final private static String wxKey = "WX.weixin_status";
	final private static String wxResult = "WX.weixin_result";
	final private static String wxCode = "WX.code";
	final private static String wxToken = "WX.token";
	final private static String wxRefreshToken = "WX.refrsh_token";
	final private static String wxOpenId = "WX.openId";
	final private static String wxiSFromLogin = "WX.from_login";
	final private static String wxiSWeiXinPay = "WX.weixin_pay";// 兼容htc1，记录微信支付方式

	/**
	 * 设置是否微信支付
	 */
	public void setIsWeiXinPay(Context context, boolean isWXPay) {
		saveBoolean(context, wxiSWeiXinPay, isWXPay);
	}

	/**
	 * 获取是否微信支付
	 */
	public boolean isWeiXinPay(Context context) {
		return getBoolean(context, wxiSWeiXinPay);
	}

	/**
	 * 设置是否来自登录
	 */
	public void setIsFromLogin(Context context, boolean isFromLogin) {
		saveBoolean(context, wxiSFromLogin, isFromLogin);
	}

	/**
	 * 获取是否来自登录
	 */
	public boolean isFromLogin(Context context) {
		return getBoolean(context, wxiSFromLogin);
	}

	// /**
	// * 保存OpenId
	// */
	// public void saveOpenId(Context context, String openId) {
	// saveString(context, wxOpenId, openId);
	// }
	//
	// /**
	// * 获取本地保存的openId
	// */
	// public String getOpenId(Context context) {
	// return getString(context, wxOpenId, null);
	// }

	/**
	 * 保存Token
	 */
	public void saveToken(Context context, String token) {
		saveString(context, wxToken, token);
	}

	/**
	 * 保存RefreshToken
	 */
	public void saveRefreshToken(Context context, String refreshToken) {
		saveString(context, wxRefreshToken, refreshToken);
	}

	/**
	 * 获取本地保存的Token
	 */
	public String getToken(Context context) {
		return getString(context, wxToken, null);
	}

	/**
	 * 获取本地保存的RefreshToken
	 */
	public String getRefreshToken(Context context) {
		return getString(context, wxRefreshToken, null);
	}

	/**
	 * 是不是微信支付返回
	 */
	public boolean isWXResult(Context context) {
		return getBoolean(context, wxKey);
	}

	/**
	 * 设置是否是微信支付
	 */
	public void setIsWX(Context context, boolean isWeixinPay) {
		saveBoolean(context, wxKey, isWeixinPay);
	}

	/**
	 * 获取微信支付返回结果
	 */
	public boolean isWXPaySuccess(Context context) {
		return getBoolean(context, wxResult);
	}

	/**
	 * 设置微信支付返回结果
	 */
	public void setWXPayResult(Context context, boolean isPaySuccess) {
		saveBoolean(context, wxResult, isPaySuccess);
	}

	/**
	 * 保存wxCode
	 */
	public void saveWXCode(Context context, String code) {
		saveString(context, wxCode, code);
	}

	/**
	 * 获取本地保存的wxCode
	 */
	public String getWXCode(Context context) {
		return getString(context, wxCode, null);
	}

	// ---------------------------------------------------------------------------
	private void saveBoolean(Context context, String key, boolean value) {
		if (context == null || key == null) {
			throw new IllegalArgumentException("SharePreferedTools.saveBoolean context or key is null");
		}
		SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	private boolean getBoolean(Context context, String key, boolean defValue) {
		if (context == null || key == null) {
			throw new IllegalArgumentException("SharePreferedTools.getInt context or key is null");
		}
		SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		return preferences.getBoolean(key, defValue);
	}

	private boolean getBoolean(Context context, String key) {
		return getBoolean(context, key, false);
	}

	// ----------------------------

	private int getInt(Context context, String key, int defValue) {
		if (context == null || key == null) {
			throw new IllegalArgumentException("SharePreferedTools.getInt context or key is null");
		}
		SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		return preferences.getInt(key, defValue);
	}

	private int getInt(Context context, String key) {
		return getInt(context, key, 0);
	}

	private void saveInt(Context context, String key, int value) {
		if (context == null || key == null) {
			throw new IllegalArgumentException("SharePreferedTools.saveInt context or key is null");
		}
		SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	// ------------
	private String getString(Context context, String key, String defValue) {
		if (context == null || key == null) {
			throw new IllegalArgumentException("SharePreferedTools.getString context or key is null");
		}
		SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		return preferences.getString(key, defValue);
	}

	private String getString(Context context, String key) {
		return getString(context, key, null);
	}

	private void saveString(Context context, String key, String value) {
		if (context == null || key == null) {
			throw new IllegalArgumentException("SharePreferedTools.saveString context or key is null");
		}
		SharedPreferences preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

}
